package com.lingjtx.auth.handle;

import com.lingjtx.common.core.constant.Const;
import com.lingjtx.common.core.exception.ResponseErrorException;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserTypeContext {

    private final List<AuthHandle> contexts = new ArrayList<>();

    public void setHandlers(AuthHandle handle) {
        contexts.add(handle);
    }

    public AuthHandle getHandle(String type) {
        for (AuthHandle context : contexts) {
            if (context.getUserType().equals(type)) {
                return context;
            }
        }
        throw new ResponseErrorException(String.format("请求头: %s 不支持: %s 类型！", Const.AuthTokenMap.USER_TYPE, type));
    }
}
